package com.jweb.plugin.jredis;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import com.jweb.plugin.Iplugin;
import com.jweb.util.FormatUtil;

/**
 * 
 * @author Yuanqy
 *
 */
public class RedisPlugin implements Iplugin {
	private static Logger logger = LoggerFactory.getLogger(RedisPlugin.class);
	private static ShardedJedisPool jspool;
	private static JedisPool jpool;
	private List<JedisShardInfo> shards;
	private JedisPoolConfig config;

	public RedisPlugin(List<JedisShardInfo> shards, JedisPoolConfig config) {
		this.shards = shards;
		this.config = config;
	}

	public RedisPlugin(String host, int port) {
		init(host, port, "", null);
	}

	public RedisPlugin(String host, int port, String name) {
		init(host, port, name, null);
	}

	public RedisPlugin(String host, int port, String name, String pwd) {
		init(host, port, name, pwd);
	}

	private void init(String host, int port, String name, String pwd) {
		List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		JedisShardInfo jsi = new JedisShardInfo(host, port, name);
		if (!FormatUtil.isEmpty(pwd))
			jsi.setPassword(pwd);
		shards.add(jsi);
		this.shards = shards;
		this.config = new JedisPoolConfig();
		this.config.setMaxTotal(20);
		this.config.setMaxIdle(5);
		this.config.setMaxWaitMillis(10000);
		this.config.setTestOnBorrow(true);
	}

	@Override
	public boolean start() {
		try {
			jspool = new ShardedJedisPool(config, shards);
			JedisShardInfo info = shards.get(0);
			jpool = new JedisPool(config, info.getHost(), info.getPort(), info.getSoTimeout(), info.getPassword());
			logger.info("[Plugin]==>Redis加载完成！");
			return true;
		} catch (Exception e) {
			logger.error("jedisPool init false ...\n" + e.getMessage());
		}
		return false;
	}

	@Override
	public boolean stop() {
		try {
			jspool.destroy();
			jspool.close();
			jpool.destroy();
			jpool.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取ShardedJedis实例
	 * 
	 * @return
	 */
	public synchronized static ShardedJedis getShardedJedis() {
		try {
			if (jspool != null) {
				ShardedJedis resource = jspool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取Jedis实例
	 * 
	 * @return
	 */
	public synchronized static Jedis getJedis() {
		try {
			if (jpool != null) {
				Jedis resource = jpool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 释放ShardedJedis资源
	 * 
	 * @param jedis
	 */
	public static void Close(final ShardedJedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}

	/**
	 * 释放jedis资源
	 * 
	 * @param jedis
	 */
	public static void Close(final Jedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}

}